#!/bin/bash
# merge file

function merge() {
	if [ -e $1 ] && [ -r $1 ] && [ -e $2 ]; then
		if [ -f $3 ]; then
			if [ -w $3 ]; then
				cat $1 $2 > $3
				echo "Release file: " $3
				cat $3
			fi
		else
			cat $1 $2 > $3
			echo "Release file: " $3
			cat $3
		fi
	else
		echo "ERROR: please check the source files are exsiting and readable, and the dist should be writable."
	fi
}

	if [ $# -gt 3 ]; then
	# too many parameters
	echo "WARNING: too many parameters and we will ignore useless parameters."
	merge $1 $2 $3
	elif [ $# -lt 1 ]; then
		# no parameter
		echo "ERROR: you should pass more than one parameter, please check and restart it."
		exit 0
	elif [ $# -eq 1 ]; then
		# only 1 parameter
		echo "ERROR: too less parameters, and the content is:"
		cat $1
	elif [ $# -eq 2 ]; then
		# 2 parameters
		echo "WARNING: lack of output file, and we will merge files to 'mydoc.txt'"
		merge $1 $2 "mydoc.txt"
	else
		# 3 parameters
		merge $1 $2 $3
fi
